package org.csu.backofficemanagersys.controller;

import com.alibaba.fastjson.JSONObject;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import lombok.extern.log4j.Log4j2;
import org.csu.backofficemanagersys.Response.ResponseMsg;
import org.csu.backofficemanagersys.base64.MD5;
import org.csu.backofficemanagersys.base64.TokenUtil;
import org.csu.backofficemanagersys.domain.Admin;
import org.csu.backofficemanagersys.service.AdminService;
import org.csu.backofficemanagersys.sms.SMSUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.security.NoSuchAlgorithmException;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/mypetstore")
@Log4j2
public class HelloController {

    @Autowired
    private MD5 md5;

    //不建议使用注解注入adminMapper,建议使用构造函数或者setter
    @Autowired
    AdminService adminService;
    @Autowired
    SMSUtils smsUtils;

    //TODO:之后会转成HTML
    @GetMapping("/index")
    public String hello(){

        return "这是开始页面";
    }
    @GetMapping("/loginForm")
    public String loginForm(){
        return null;
    }
    @GetMapping("/code/{phone}")
    
    public String getPhoneCode(HttpServletRequest request, @PathVariable String phone) throws NoSuchAlgorithmException {
        String code = smsUtils.Code();
        code = md5.encoder(code).toString();
        request.getSession().setAttribute("phoneCode",code);

        smsUtils.sendMsg(phone,code);
        return code;
    }

    @PostMapping("/login")
    
    //TODO:这里需要做校验
    //从前端传入的就是base64加密之后的密码和账号，但是我们的账号是字符串，不过问题应该不大
    public ResponseMsg<JSONObject> login(HttpServletRequest request, String adminname, String password) throws IOException {
        log.info(adminname+" and "+password);
        String test = request.getReader().lines().collect(Collectors.joining(System.lineSeparator()));
        log.info(test);
        Admin admin = adminService.selectOneByNameAndPassword(adminname,password);
        if(admin == null){
            request.getSession().setAttribute("isLogin",0);
            return ResponseMsg.createForError();
        }else{
            JSONObject jsonObject = new JSONObject();
            request.getSession().setAttribute("isLogin",1);
            HttpSession session = request.getSession();
            session.setAttribute("loginAdmin",admin);
            String token = TokenUtil.sign(admin);
            jsonObject.put("token",token);
            jsonObject.put("admin",admin);
            jsonObject.put("msg","登录成功");
            return ResponseMsg.createForSuccess(jsonObject);
        }
    }
    @PostMapping("/loginphone")
    
    //TODO:这里需要做校验
    //从前端传入的就是md5加密之后的密码和账号，但是我们的账号是字符串，不过问题应该不大
    public ResponseMsg<JSONObject> loginPhone(HttpServletRequest request, String phone, String code) {
        Admin admin = adminService.selectByPhone(phone);
        String code1 =(String) request.getSession().getAttribute("code");
        if(admin != null && code1.equals(code)){
            JSONObject jsonObject = new JSONObject();
            request.getSession().setAttribute("isLogin",1);
            HttpSession session = request.getSession();
            session.setAttribute("loginAdmin",admin);
            String token = TokenUtil.sign(admin);
            jsonObject.put("token",token);
            jsonObject.put("admin",admin);
            jsonObject.put("msg","登录成功");
            return ResponseMsg.createForSuccess(jsonObject);
        }
        request.getSession().setAttribute("isLogin",0);
        return ResponseMsg.createForError();
    }

}
